package com.frxuexi.park2301.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.entity.NoticeBeanItem
import com.frxuexi.lib_base.mvvm.BaseFragment
import com.frxuexi.park2301.R
import com.frxuexi.park2301.databinding.FragmentHomeBinding
import com.frxuexi.park2301.notice.NoticeAdapter
import com.frxuexi.park2301.notice.NoticeViewModel
import com.frxuexi.park2301.notice.OnItemClickListener
import com.frxuexi.park2301.notice.OnItemLongClickListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding,NoticeViewModel>() {
    val noticeAdapter by lazy { NoticeAdapter() }
    override fun initData() {

    }

    override fun initView() {
        binding.rvNotice.apply {
            adapter=noticeAdapter
            layoutManager=LinearLayoutManager(requireContext())
        }

        noticeAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(position: Int, item: NoticeBeanItem) {
                Log.i("===","${position},${item.toString()}")
            }

        })

        noticeAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(item: NoticeBeanItem): Boolean {
                Log.i("===","${item.toString()}")
                return true
            }

        })
        lifecycleScope.launch {
            vm.getNotices().collect{
                noticeAdapter.submitData(it)
            }
        }

        binding.llSign.setOnClickListener {
            ARouter.getInstance().build("/sign/SignActivity").navigation()
        }
    }


}